
package Chatter::API::Controller::users;

use Moose;
use Date::Simple ('date', 'today');
use Data::Dumper;
BEGIN { extends 'Catalyst::Controller'; }

# ユーザ一覧照会
sub root :Path('') {
  my ($self, $c) = @_;

  my $users = $c->stash->{tAccount};
  my @jsons;
  while(my $user = $users->next){
    my $json;
    $json->{accountId}  = $user->accountId;
    $json->{name}  = $user->name;
    $json->{email} = $user->email;
    $json->{followers} = $user->followers;
    $json->{followings} = $user->followings;
    $json->{url} = $user->url;
    $json->{oldest} = $user->oldest =~ /0000/ ? undef : $user->oldest;
    $json->{latest} = $user->latest =~ /0000/ ? undef : $user->latest;
    $json->{count} = $user->count;
    push(@jsons,$json);
  }
  $c->stash->{users} = \@jsons;
  $c->forward('View::JSON');
}

# ユーザ個別照会
sub default :LocalRegex('^(.*)$') {
  my ($self, $c) = @_;
  my $id = $c->req->captures->[0];

  # ユーザ検索
  my $user = $c->stash->{tAccount}->single(
    { email => $id }
  );

  if($user){
    my $json;
    $json->{accountId}  = $user->accountId;
    $json->{name}  = $user->name;
    $json->{email} = $user->email;
    $json->{followers} = $user->followers;
    $json->{followings} = $user->followings;
    $json->{url} = $user->url;
    $json->{oldest} = $user->oldest =~ /0000/ ? undef : $user->oldest;
    $json->{latest} = $user->latest =~ /0000/ ? undef : $user->latest;
    $json->{count} = $user->count;
    $c->stash->{user} = $json;
  } else {
    $c->stash->{user} = undef;
  }
  $c->log->debug(Dumper($c->stash->{user}));
  $c->forward('View::JSON');
}




__PACKAGE__->meta->make_immutable;
1;

